package com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.entity.SmsPurchaseRequisitionProcess;
import com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.entity.dto.SmsPurchaseRequisitionProcessDto;
import com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.entity.vo.SmsPurchaseRequisitionProcessVo;
import com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.entity.vo.SmsPurchaseRequisitionUserVo;
import com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.mapper.SmsPurchaseRequisitionProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.service.SmsPurchaseRequisitionProcessService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.constant.RequisitionObjectTypeConstant.REQUISITION_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.purchaseRequisition.constant.RequisitionObjectTypeConstant.REQUISITION_OBJECT_TYPE_ROLE;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-06-05
 */
@Service
public class SmsPurchaseRequisitionProcessServiceImpl extends ServiceImpl<SmsPurchaseRequisitionProcessMapper, SmsPurchaseRequisitionProcess> implements SmsPurchaseRequisitionProcessService {

    @Resource
    private SmsPurchaseRequisitionProcessMapper smsPurchaseRequisitionProcessMapper;
    @Resource
    private CacheUtil cacheUtil;
    @Resource
    private SmsRoleMapper smsRoleMapper;

    @Override
    public ResponseResult getRequisitionProcessList() {
        List<SmsPurchaseRequisitionProcessVo> processList = smsPurchaseRequisitionProcessMapper.selectList(new QueryWrapper<SmsPurchaseRequisitionProcess>()
        .lambda()
        .orderByAsc(SmsPurchaseRequisitionProcess::getProcessSort))
                .stream()
                .map(process ->{
                    SmsPurchaseRequisitionProcessVo smsPurchaseRequisitionProcessVo = new SmsPurchaseRequisitionProcessVo();
                    BeanUtils.copyProperties(process,smsPurchaseRequisitionProcessVo);
                    if(process.getProcessObjectType() != null && !isNullOrEmpty(process.getProcessObjectId())) {
                        if (process.getProcessObjectType() == REQUISITION_OBJECT_TYPE_PERSON) {
                            SmsUser userInfo = cacheUtil.getUserInfo(process.getProcessObjectId());
                            if (userInfo != null && !isNullOrEmpty(userInfo.getId())) {
                                SmsPurchaseRequisitionUserVo userVo = new SmsPurchaseRequisitionUserVo();
                                BeanUtils.copyProperties(userInfo,userVo);
                                smsPurchaseRequisitionProcessVo.setUserInfo(userVo);
                                smsPurchaseRequisitionProcessVo.setProcessObjectName(userInfo.getRealName());
                            }
                        } else {
                            SmsRole smsRole = smsRoleMapper.selectById(process.getProcessObjectId());
                            if (smsRole != null && !isNullOrEmpty(smsRole.getId())) {
                                smsPurchaseRequisitionProcessVo.setProcessObjectName(smsRole.getRoleName());
                            }
                        }
                    }
                    return smsPurchaseRequisitionProcessVo;
                })
                .collect(Collectors.toList());

        return CommonResult.success(processList);
    }

    @Override
    public ResponseResult updateRequisitionProcess(SmsPurchaseRequisitionProcessDto updateProcessDto) {
        if(isNullOrEmpty(updateProcessDto.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if(isNullOrEmpty(updateProcessDto.getProcessObjectType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //如果在可编辑情况下
        //审批对象类型不为空时,审批对象id可为空
        if (updateProcessDto.getProcessIsApplicantEdit()) {
            if (updateProcessDto.getProcessObjectType() == REQUISITION_OBJECT_TYPE_ROLE) {
                if (isNullOrEmpty(updateProcessDto.getProcessObjectId())) {
                    return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
                }
            }
        } else {
            if (isNullOrEmpty(updateProcessDto.getProcessObjectId())) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
        }

        SmsPurchaseRequisitionProcess approvalProcess = smsPurchaseRequisitionProcessMapper.selectById(updateProcessDto.getId());
        //判断审批流程是否存在
        if(approvalProcess == null || isNullOrEmpty(approvalProcess.getId())){
            return CommonResult.failed(CommonCodeEnum.PURCHASE_REQUISITION_PROCESS_NOT_EXIST);
        }
        //判断角色或个人是否存在
        if(updateProcessDto.getProcessObjectType() != null && !isNullOrEmpty(updateProcessDto.getProcessObjectId())){
            if(updateProcessDto.getProcessObjectType() == REQUISITION_OBJECT_TYPE_PERSON){
                SmsUser userInfo = cacheUtil.getUserInfo(updateProcessDto.getProcessObjectId());
                if(userInfo == null || isNullOrEmpty(userInfo.getId())){
                    return CommonResult.failed(CommonCodeEnum.PURCHASE_REQUISITION_PROCESS_USER_NOT_EXIST);
                }
            }else if(updateProcessDto.getProcessObjectType() == REQUISITION_OBJECT_TYPE_ROLE){
                SmsRole smsRole = smsRoleMapper.selectById(updateProcessDto.getProcessObjectId());
                if(smsRole == null || isNullOrEmpty(smsRole.getId())){
                    return CommonResult.failed(CommonCodeEnum.PURCHASE_REQUISITION_PROCESS_ROLE_NOT_EXIST);
                }
            }
        }
        SmsPurchaseRequisitionProcess updateProcess = new SmsPurchaseRequisitionProcess();
        updateProcess.setId(updateProcessDto.getId());
        if (!isNullOrEmpty(updateProcessDto.getProcessObjectType())) {
            updateProcess.setProcessObjectId(updateProcessDto.getProcessObjectId());
            updateProcess.setProcessObjectType(updateProcessDto.getProcessObjectType());
        }
        if (!isNullOrEmpty(updateProcessDto.getProcessIsApplicantEdit())) {
            updateProcess.setProcessIsApplicantEdit(updateProcessDto.getProcessIsApplicantEdit());
        }
        if (!isNullOrEmpty(updateProcessDto.getProcessIsSign())) {
            updateProcess.setProcessIsSign(updateProcessDto.getProcessIsSign());
        }
        if (!isNullOrEmpty(updateProcessDto.getProcessIsStamp())) {
            updateProcess.setProcessIsStamp(updateProcessDto.getProcessIsStamp());
        }
        if (!isNullOrEmpty(updateProcessDto.getProcessIsUse())) {
            updateProcess.setProcessIsUse(updateProcessDto.getProcessIsUse());
        }

        smsPurchaseRequisitionProcessMapper.updateById(updateProcess);
        return CommonResult.success();
    }
}
